1 /*******************************************************************************
2  * Copyright (c) 2000, 2020 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15 
16 import java.util.*;
17 import java.util.List;
18 
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.widgets.*;
22 
23 class DefaultContent implements StyledTextContent {
24 	private final static String LineDelimiter = System.lineSeparator();
25 
26 	List<StyledTextListener> textListeners = new ArrayList<>(); // stores text listeners for event sending
27 	char[] textStore = new char[0];	// stores the actual text
28 	int gapStart = -1;	// the character position start of the gap
29 	int gapEnd = -1;	// the character position after the end of the gap
30 	int gapLine = -1;	// the line on which the gap exists, the gap will always be associated with one line
31 	int highWatermark = 300;
32 	int lowWatermark = 50;
33 
34 	int[][] lines = new int[50][2];	// array of character positions and lengths representing the lines of text
35 	int lineCount = 0;	// the number of lines of text
36 	int expandExp = 1; 	// the expansion exponent, used to increase the lines array exponentially
37 	int replaceExpandExp = 1; 	// the expansion exponent, used to increase the lines array exponentially
38 
39 /**
40  * Creates a new DefaultContent and initializes it.  A <code>StyledTextContent</code> will always have
41  * at least one empty line.
42  */
DefaultContent()43 DefaultContent() {
44 	super();
45 	setText("");
46 }
47 /**
48  * Adds a line to the end of the line indexes array.  Increases the size of the array if necessary.
49  * <code>lineCount</code> is updated to reflect the new entry.
50  * <p>
51  *
52  * @param start the start of the line
53  * @param length the length of the line
54  */
addLineIndex(int start, int length)55 void addLineIndex(int start, int length) {
56 	int size = lines.length;
57 	if (lineCount == size) {
58 		// expand the lines by powers of 2
59 		int[][] newLines = new int[size+Compatibility.pow2(expandExp)][2];
60 		System.arraycopy(lines, 0, newLines, 0, size);
61 		lines = newLines;
62 		expandExp++;
63 	}
64 	int[] range = new int[] {start, length};
65 	lines[lineCount] = range;
66 	lineCount++;
67 }
68 /**
69  * Adds a line index to the end of <code>linesArray</code>.  Increases the
70  * size of the array if necessary and returns a new array.
71  * <p>
72  *
73  * @param start the start of the line
74  * @param length the length of the line
75  * @param linesArray the array to which to add the line index
76  * @param count the position at which to add the line
77  * @return a new array of line indexes
78  */
addLineIndex(int start, int length, int[][] linesArray, int count)79 int[][] addLineIndex(int start, int length, int[][] linesArray, int count) {
80 	int size = linesArray.length;
81 	int[][] newLines = linesArray;
82 	if (count == size) {
83 		newLines = new int[size+Compatibility.pow2(replaceExpandExp)][2];
84 		replaceExpandExp++;
85 		System.arraycopy(linesArray, 0, newLines, 0, size);
86 	}
87 	int[] range = new int[] {start, length};
88 	newLines[count] = range;
89 	return newLines;
90 }
91 /**
92  * Adds a <code>TextChangeListener</code> listening for
93  * <code>TextChangingEvent</code> and <code>TextChangedEvent</code>. A
94  * <code>TextChangingEvent</code> is sent before changes to the text occur.
95  * A <code>TextChangedEvent</code> is sent after changes to the text
96  * occurred.
97  * <p>
98  *
99  * @param listener the listener
100  * @exception IllegalArgumentException <ul>
101  *    <li>ERROR_NULL_ARGUMENT when listener is null</li>
102  * </ul>
103  */
104 @Override
addTextChangeListener(TextChangeListener listener)105 public void addTextChangeListener(TextChangeListener listener) {
106 	if (listener == null) error(SWT.ERROR_NULL_ARGUMENT);
107 	StyledTextListener typedListener = new StyledTextListener(listener);
108 	textListeners.add(typedListener);
109 }
110 /**
111  * Adjusts the gap to accommodate a text change that is occurring.
112  * <p>
113  *
114  * @param position the position at which a change is occurring
115  * @param sizeHint the size of the change
116  * @param line the line where the gap will go
117  */
adjustGap(int position, int sizeHint, int line)118 void adjustGap(int position, int sizeHint, int line) {
119 	if (position == gapStart) {
120 		// text is being inserted at the gap position
121 		int size = (gapEnd - gapStart) - sizeHint;
122 		if (lowWatermark <= size && size <= highWatermark)
123 			return;
124 	} else if ((position + sizeHint == gapStart) && (sizeHint < 0)) {
125 		// text is being deleted at the gap position
126 		int size = (gapEnd - gapStart) - sizeHint;
127 		if (lowWatermark <= size && size <= highWatermark)
128 			return;
129 	}
130 	moveAndResizeGap(position, sizeHint, line);
131 }
132 /**
133  * Calculates the indexes of each line in the text store.  Assumes no gap exists.
134  * Optimized to do less checking.
135  */
indexLines()136 void indexLines(){
137 	int start = 0;
138 	lineCount = 0;
139 	int textLength = textStore.length;
140 	int i;
141 	for (i = start; i < textLength; i++) {
142 		char ch = textStore[i];
143 		if (ch == SWT.CR) {
144 			// see if the next character is a LF
145 			if (i + 1 < textLength) {
146 				ch = textStore[i+1];
147 				if (ch == SWT.LF) {
148 					i++;
149 				}
150 			}
151 			addLineIndex(start, i - start + 1);
152 			start = i + 1;
153 		} else if (ch == SWT.LF) {
154 			addLineIndex(start, i - start + 1);
155 			start = i + 1;
156 		}
157 	}
158 	addLineIndex(start, i - start);
159 }
160 /**
161  * Returns whether or not the given character is a line delimiter.  Both CR and LF
162  * are valid line delimiters.
163  * <p>
164  *
165  * @param ch the character to test
166  * @return true if ch is a delimiter, false otherwise
167  */
isDelimiter(char ch)168 boolean isDelimiter(char ch) {
169 	if (ch == SWT.CR) return true;
170 	if (ch == SWT.LF) return true;
171 	return false;
172 }
173 /**
174  * Determine whether or not the replace operation is valid.  DefaultContent will not allow
175  * the /r/n line delimiter to be split or partially deleted.
176  * <p>
177  *
178  * @param start	start offset of text to replace
179  * @param replaceLength start offset of text to replace
180  * @param newText start offset of text to replace
181  * @return a boolean specifying whether or not the replace operation is valid
182  */
isValidReplace(int start, int replaceLength, String newText)183 protected boolean isValidReplace(int start, int replaceLength, String newText){
184 	if (replaceLength == 0) {
185 		// inserting text, see if the \r\n line delimiter is being split
186 		if (start == 0) return true;
187 		if (start == getCharCount()) return true;
188 		char before = getTextRange(start - 1, 1).charAt(0);
189 		if (before == '\r') {
190 			char after = getTextRange(start, 1).charAt(0);
191 			if (after == '\n') return false;
192 		}
193 	} else {
194 		// deleting text, see if part of a \r\n line delimiter is being deleted
195 		char startChar = getTextRange(start, 1).charAt(0);
196 		if (startChar == '\n') {
197 			// see if char before delete position is \r
198 			if (start != 0) {
199 				char before = getTextRange(start - 1, 1).charAt(0);
200 				if (before == '\r') return false;
201 			}
202 		}
203 		char endChar = getTextRange(start + replaceLength - 1, 1).charAt(0);
204 		if (endChar == '\r') {
205 			// see if char after delete position is \n
206 			if (start + replaceLength != getCharCount()) {
207 				char after = getTextRange(start + replaceLength, 1).charAt(0);
208 				if (after == '\n') return false;
209 			}
210 		}
211 	}
212 	return true;
213 }
214 /**
215  * Calculates the indexes of each line of text in the given range.
216  * <p>
217  *
218  * @param offset the logical start offset of the text lineate
219  * @param length the length of the text to lineate, includes gap
220  * @param numLines the number of lines to initially allocate for the line index array,
221  *	passed in for efficiency (the exact number of lines may be known)
222  * @return a line indexes array where each line is identified by a start offset and
223  * 	a length
224  */
indexLines(int offset, int length, int numLines)225 int[][] indexLines(int offset, int length, int numLines){
226 	int[][] indexedLines = new int[numLines][2];
227 	int start = 0;
228 	int lineCount = 0;
229 	int i;
230 	replaceExpandExp = 1;
231 	for (i = start; i < length; i++) {
232 		int location = i + offset;
233 		if ((location >= gapStart) && (location < gapEnd)) {
234 			// ignore the gap
235 		} else {
236 			char ch = textStore[location];
237 			if (ch == SWT.CR) {
238 				// see if the next character is a LF
239 				if (location+1 < textStore.length) {
240 					ch = textStore[location+1];
241 					if (ch == SWT.LF) {
242 						i++;
243 					}
244 				}
245 				indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount);
246 				lineCount++;
247 				start = i + 1;
248 			} else if (ch == SWT.LF) {
249 				indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount);
250 				lineCount++;
251 				start = i + 1;
252 			}
253 		}
254 	}
255 	int[][] newLines = new int[lineCount+1][2];
256 	System.arraycopy(indexedLines, 0, newLines, 0, lineCount);
257 	int[] range = new int[] {start, i - start};
258 	newLines[lineCount] = range;
259 	return newLines;
260 }
261 /**
262  * Inserts text.
263  * <p>
264  *
265  * @param position the position at which to insert the text
266  * @param text the text to insert
267  */
insert(int position, String text)268 void insert(int position, String text) {
269 	if (text.length() == 0) return;
270 
271 	int startLine = getLineAtOffset(position);
272 	int change = text.length();
273 	boolean endInsert = position == getCharCount();
274 	adjustGap(position, change, startLine);
275 
276 	// during an insert the gap will be adjusted to start at
277 	// position and it will be associated with startline, the
278 	// inserted text will be placed in the gap
279 	int startLineOffset = getOffsetAtLine(startLine);
280 	// at this point, startLineLength will include the start line
281 	// and all of the newly inserted text
282 	int	startLineLength = getPhysicalLine(startLine).length();
283 
284 	if (change > 0) {
285 		// shrink gap
286 		gapStart += (change);
287 		for (int i = 0; i < text.length(); i++) {
288 			textStore[position + i]= text.charAt(i);
289 		}
290 	}
291 
292 	// figure out the number of new lines that have been inserted
293 	int [][] newLines = indexLines(startLineOffset, startLineLength, 10);
294 	// only insert an empty line if it is the last line in the text
295 	int numNewLines = newLines.length - 1;
296 	if (newLines[numNewLines][1] == 0) {
297 		// last inserted line is a new line
298 		if (endInsert) {
299 			// insert happening at end of the text, leave numNewLines as
300 			// is since the last new line will not be concatenated with another
301 			// line
302 			numNewLines += 1;
303 		} else {
304 			numNewLines -= 1;
305 		}
306 	}
307 
308 	// make room for the new lines
309 	expandLinesBy(numNewLines);
310 	// shift down the lines after the replace line
311 	for (int i = lineCount - 1; i > startLine; i--) {
312 		lines[i + numNewLines]=lines[i];
313 	}
314 	// insert the new lines
315 	for (int i = 0; i < numNewLines; i++) {
316 		newLines[i][0] += startLineOffset;
317 		lines[startLine + i]=newLines[i];
318 	}
319 	// update the last inserted line
320 	if (numNewLines < newLines.length) {
321 		newLines[numNewLines][0] += startLineOffset;
322 		lines[startLine + numNewLines] = newLines[numNewLines];
323 	}
324 
325 	lineCount += numNewLines;
326 	gapLine = getLineAtPhysicalOffset(gapStart);
327 }
328 /**
329  * Moves the gap and adjusts its size in anticipation of a text change.
330  * The gap is resized to actual size + the specified size and moved to the given
331  * position.
332  * <p>
333  *
334  * @param position the position at which a change is occurring
335  * @param size the size of the change
336  * @param newGapLine the line where the gap should be put
337  */
moveAndResizeGap(int position, int size, int newGapLine)338 void moveAndResizeGap(int position, int size, int newGapLine) {
339 	char[] content = null;
340 	int oldSize = gapEnd - gapStart;
341 	int newSize;
342 	if (size > 0) {
343 		newSize = highWatermark + size;
344 	} else {
345 		newSize = lowWatermark - size;
346 	}
347 	// remove the old gap from the lines information
348 	if (gapExists()) {
349 		// adjust the line length
350 		lines[gapLine][1] = lines[gapLine][1] - oldSize;
351 		// adjust the offsets of the lines after the gapLine
352 		for (int i = gapLine + 1; i < lineCount; i++) {
353 			lines[i][0] = lines[i][0] - oldSize;
354 		}
355 	}
356 
357 	if (newSize < 0) {
358 		if (oldSize > 0) {
359 			// removing the gap
360 			content = new char[textStore.length - oldSize];
361 			System.arraycopy(textStore, 0, content, 0, gapStart);
362 			System.arraycopy(textStore, gapEnd, content, gapStart, content.length - gapStart);
363 			textStore = content;
364 		}
365 		gapStart = gapEnd = position;
366 		return;
367 	}
368 	content = new char[textStore.length + (newSize - oldSize)];
369 	int newGapStart = position;
370 	int newGapEnd = newGapStart + newSize;
371 	if (oldSize == 0) {
372 		System.arraycopy(textStore, 0, content, 0, newGapStart);
373 		System.arraycopy(textStore, newGapStart, content, newGapEnd, content.length - newGapEnd);
374 	} else if (newGapStart < gapStart) {
375 		int delta = gapStart - newGapStart;
376 		System.arraycopy(textStore, 0, content, 0, newGapStart);
377 		System.arraycopy(textStore, newGapStart, content, newGapEnd, delta);
378 		System.arraycopy(textStore, gapEnd, content, newGapEnd + delta, textStore.length - gapEnd);
379 	} else {
380 		int delta = newGapStart - gapStart;
381 		System.arraycopy(textStore, 0, content, 0, gapStart);
382 		System.arraycopy(textStore, gapEnd, content, gapStart, delta);
383 		System.arraycopy(textStore, gapEnd + delta, content, newGapEnd, content.length - newGapEnd);
384 	}
385 	textStore = content;
386 	gapStart = newGapStart;
387 	gapEnd = newGapEnd;
388 
389 	// add the new gap to the lines information
390 	if (gapExists()) {
391 		gapLine = newGapLine;
392 		// adjust the line length
393 		int gapLength = gapEnd - gapStart;
394 		lines[gapLine][1] = lines[gapLine][1] + (gapLength);
395 		// adjust the offsets of the lines after the gapLine
396 		for (int i = gapLine + 1; i < lineCount; i++) {
397 			lines[i][0] = lines[i][0] + gapLength;
398 		}
399 	}
400 }
401 /**
402  * Returns the number of lines that are in the specified text.
403  * <p>
404  *
405  * @param startOffset the start of the text to lineate
406  * @param length the length of the text to lineate
407  * @return number of lines
408  */
lineCount(int startOffset, int length)409 int lineCount(int startOffset, int length){
410 	if (length == 0) {
411 		return 0;
412 	}
413 	int lineCount = 0;
414 	int count = 0;
415 	int i = startOffset;
416 	if (i >= gapStart) {
417 		i += gapEnd - gapStart;
418 	}
419 	while (count < length) {
420 		if ((i >= gapStart) && (i < gapEnd)) {
421 			// ignore the gap
422 		} else {
423 			char ch = textStore[i];
424 			if (ch == SWT.CR) {
425 				// see if the next character is a LF
426 				if (i + 1 < textStore.length) {
427 					ch = textStore[i+1];
428 					if (ch == SWT.LF) {
429 						i++;
430 						count++;
431 					}
432 				}
433 				lineCount++;
434 			} else if (ch == SWT.LF) {
435 				lineCount++;
436 			}
437 			count++;
438 		}
439 		i++;
440 	}
441 	return lineCount;
442 }
443 /**
444  * Returns the number of lines that are in the specified text.
445  * <p>
446  *
447  * @param text the text to lineate
448  * @return number of lines in the text
449  */
lineCount(String text)450 int lineCount(String text){
451 	int lineCount = 0;
452 	int length = text.length();
453 	for (int i = 0; i < length; i++) {
454 		char ch = text.charAt(i);
455 		if (ch == SWT.CR) {
456 			if (i + 1 < length && text.charAt(i + 1) == SWT.LF) {
457 				i++;
458 			}
459 			lineCount++;
460 		} else if (ch == SWT.LF) {
461 			lineCount++;
462 		}
463 	}
464 	return lineCount;
465 }
466 /**
467  * @return the logical length of the text store
468  */
469 @Override
getCharCount()470 public int getCharCount() {
471 	int length = gapEnd - gapStart;
472 	return (textStore.length - length);
473 }
474 /**
475  * Returns the line at <code>index</code> without delimiters.
476  * <p>
477  *
478  * @param index	the index of the line to return
479  * @return the logical line text (i.e., without the gap)
480  * @exception IllegalArgumentException <ul>
481  *   <li>ERROR_INVALID_ARGUMENT when index is out of range</li>
482  * </ul>
483  */
484 @Override
getLine(int index)485 public String getLine(int index) {
486 	if ((index >= lineCount) || (index < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
487 	int start = lines[index][0];
488 	int length = lines[index][1];
489 	int end = start + length - 1;
490 	if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
491 		// line is before or after the gap
492 		while ((length - 1 >= 0) && isDelimiter(textStore[start+length-1])) {
493 			length--;
494 		}
495 		return new String(textStore, start, length);
496 	} else {
497 		// gap is in the specified range, strip out the gap
498 		StringBuilder buf = new StringBuilder();
499 		int gapLength = gapEnd - gapStart;
500 		buf.append(textStore, start, gapStart - start);
501 		buf.append(textStore, gapEnd, length - gapLength - (gapStart - start));
502 		length = buf.length();
503 		while ((length - 1 >=0) && isDelimiter(buf.charAt(length - 1))) {
504 			length--;
505 		}
506 		return buf.toString().substring(0, length);
507 	}
508 }
509 /**
510  * Returns the line delimiter that should be used by the StyledText
511  * widget when inserting new lines.  This delimiter may be different than the
512  * delimiter that is used by the <code>StyledTextContent</code> interface.
513  * <p>
514  *
515  * @return the platform line delimiter as specified in the line.separator
516  * 	system property.
517  */
518 @Override
getLineDelimiter()519 public String getLineDelimiter() {
520 	return LineDelimiter;
521 }
522 /**
523  * Returns the line at the given index with delimiters.
524  * <p>
525  * @param index	the index of the line to return
526  * @return the logical line text (i.e., without the gap) with delimiters
527  */
getFullLine(int index)528 String getFullLine(int index) {
529 	int start = lines[index][0];
530 	int length = lines[index][1];
531 	int end = start + length - 1;
532 	if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
533 		// line is before or after the gap
534 		return new String(textStore, start, length);
535 	} else {
536 		// gap is in the specified range, strip out the gap
537 		StringBuilder buffer = new StringBuilder();
538 		int gapLength = gapEnd - gapStart;
539 		buffer.append(textStore, start, gapStart - start);
540 		buffer.append(textStore, gapEnd, length - gapLength - (gapStart - start));
541 		return buffer.toString();
542 	}
543 }
544 /**
545  * Returns the physical line at the given index (i.e., with delimiters and the gap).
546  * <p>
547  *
548  * @param index the line index
549  * @return the physical line
550  */
getPhysicalLine(int index)551 String getPhysicalLine(int index) {
552 	int start = lines[index][0];
553 	int length = lines[index][1];
554 	return getPhysicalText(start, length);
555 }
556 /**
557  * @return the number of lines in the text store
558  */
559 @Override
getLineCount()560 public int getLineCount(){
561 	return lineCount;
562 }
563 /**
564  * Returns the line at the given offset.
565  * <p>
566  *
567  * @param charPosition logical character offset (i.e., does not include gap)
568  * @return the line index
569  * @exception IllegalArgumentException <ul>
570  *    <li>ERROR_INVALID_ARGUMENT when charPosition is out of range</li>
571  * </ul>
572  */
573 @Override
getLineAtOffset(int charPosition)574 public int getLineAtOffset(int charPosition){
575 	if ((charPosition > getCharCount()) || (charPosition < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
576 	int position;
577 	if (charPosition < gapStart) {
578 		// position is before the gap
579 		position = charPosition;
580 	} else {
581 		// position includes the gap
582 		position = charPosition + (gapEnd - gapStart);
583 	}
584 
585 	// if last line and the line is not empty you can ask for
586 	// a position that doesn't exist (the one to the right of the
587 	// last character) - for inserting
588 	if (lineCount > 0) {
589 		int lastLine = lineCount - 1;
590 		if (position == lines[lastLine][0] + lines[lastLine][1])
591 			return lastLine;
592 	}
593 
594 	int high = lineCount;
595 	int low = -1;
596 	int index = lineCount;
597 	while (high - low > 1) {
598 		index = (high + low) / 2;
599 		int lineStart = lines[index][0];
600 		int lineEnd = lineStart + lines[index][1] - 1;
601 		if (position <= lineStart) {
602 			high = index;
603 		} else if (position <= lineEnd) {
604 			high = index;
605 			break;
606 		} else {
607 			low = index;
608 		}
609 	}
610 	return high;
611 }
612 /**
613  * Returns the line index at the given physical offset.
614  * <p>
615  *
616  * @param position physical character offset (i.e., includes gap)
617  * @return the line index
618  */
getLineAtPhysicalOffset(int position)619 int getLineAtPhysicalOffset(int position){
620 	int high = lineCount;
621 	int low = -1;
622 	int index = lineCount;
623 	while (high - low > 1) {
624 		index = (high + low) / 2;
625 		int lineStart = lines[index][0];
626 		int lineEnd = lineStart + lines[index][1] - 1;
627 		if (position <= lineStart) {
628 			high = index;
629 		} else if (position <= lineEnd) {
630 			high = index;
631 			break;
632 		} else {
633 			low = index;
634 		}
635 	}
636 	return high;
637 }
638 /**
639  * Returns the logical offset of the given line.
640  * <p>
641  *
642  * @param lineIndex index of line
643  * @return the logical starting offset of the line.  When there are not any lines,
644  * 	getOffsetAtLine(0) is a valid call that should answer 0.
645  * @exception IllegalArgumentException <ul>
646  *   <li>ERROR_INVALID_ARGUMENT when lineIndex is out of range</li>
647  * </ul>
648  */
649 @Override
getOffsetAtLine(int lineIndex)650 public int getOffsetAtLine(int lineIndex) {
651 	if (lineIndex == 0) return 0;
652 	if ((lineIndex >= lineCount) || (lineIndex < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
653 	int start = lines[lineIndex][0];
654 	if (start > gapEnd) {
655 		return start - (gapEnd - gapStart);
656 	} else {
657 		return start;
658 	}
659 }
660 /**
661  * Increases the line indexes array to accommodate more lines.
662  * <p>
663  *
664  * @param numLines the number to increase the array by
665  */
expandLinesBy(int numLines)666 void expandLinesBy(int numLines) {
667 	int size = lines.length;
668 	if (size - lineCount >= numLines) {
669 		return;
670 	}
671 	int[][] newLines = new int[size+Math.max(10, numLines)][2];
672 	System.arraycopy(lines, 0, newLines, 0, size);
673 	lines = newLines;
674 }
675 /**
676  * Reports an SWT error.
677  * <p>
678  *
679  * @param code the error code
680  */
error(int code)681 void error (int code) {
682 	SWT.error(code);
683 }
684 /**
685  * Returns whether or not a gap exists in the text store.
686  * <p>
687  *
688  * @return true if gap exists, false otherwise
689  */
gapExists()690 boolean gapExists() {
691 	return gapStart != gapEnd;
692 }
693 /**
694  * Returns a string representing the continuous content of
695  * the text store.
696  * <p>
697  *
698  * @param start	the physical start offset of the text to return
699  * @param length the physical length of the text to return
700  * @return the text
701  */
getPhysicalText(int start, int length)702 String getPhysicalText(int start, int length) {
703 	return new String(textStore, start, length);
704 }
705 /**
706  * Returns a string representing the logical content of
707  * the text store (i.e., gap stripped out).
708  * <p>
709  *
710  * @param start the logical start offset of the text to return
711  * @param length the logical length of the text to return
712  * @return the text
713  */
714 @Override
getTextRange(int start, int length)715 public String getTextRange(int start, int length) {
716 	if (textStore == null)
717 		return "";
718 	if (length == 0)
719 		return "";
720 	int end= start + length;
721 	if (!gapExists() || (end < gapStart))
722 		return new String(textStore, start, length);
723 	if (gapStart < start) {
724 		int gapLength= gapEnd - gapStart;
725 		return new String(textStore, start + gapLength , length);
726 	}
727 	StringBuilder buf = new StringBuilder();
728 	buf.append(textStore, start, gapStart - start);
729 	buf.append(textStore, gapEnd, end - gapStart);
730 	return buf.toString();
731 }
732 /**
733  * Removes the specified <code>TextChangeListener</code>.
734  * <p>
735  *
736  * @param listener the listener which should no longer be notified
737  *
738  * @exception IllegalArgumentException <ul>
739  *    <li>ERROR_NULL_ARGUMENT when listener is null</li>
740  * </ul>
741  */
742 @Override
removeTextChangeListener(TextChangeListener listener)743 public void removeTextChangeListener(TextChangeListener listener){
744 	if (listener == null) error(SWT.ERROR_NULL_ARGUMENT);
745 	for (int i = 0; i < textListeners.size(); i++) {
746 		TypedListener typedListener = textListeners.get(i);
747 		if (typedListener.getEventListener () == listener) {
748 			textListeners.remove(i);
749 			break;
750 		}
751 	}
752 }
753 /**
754  * Replaces the text with <code>newText</code> starting at position <code>start</code>
755  * for a length of <code>replaceLength</code>.  Notifies the appropriate listeners.
756  * <p>
757  *
758  * When sending the TextChangingEvent, <code>newLineCount</code> is the number of
759  * lines that are going to be inserted and <code>replaceLineCount</code> is
760  * the number of lines that are going to be deleted, based on the change
761  * that occurs visually.  For example:
762  * </p>
763  * <ul>
764  * <li>(replaceText,newText) ==&gt; (replaceLineCount,newLineCount)
765  * <li>("","\n") ==&gt; (0,1)
766  * <li>("\n\n","a") ==&gt; (2,0)
767  * </ul>
768  *
769  * @param start	start offset of text to replace
770  * @param replaceLength start offset of text to replace
771  * @param newText start offset of text to replace
772  *
773  * @exception SWTException <ul>
774  *   <li>ERROR_INVALID_ARGUMENT when the text change results in a multi byte
775  *      line delimiter being split or partially deleted.  Splitting a line
776  *      delimiter by inserting text between the CR and LF characters of the
777  *      \r\n delimiter or deleting part of this line delimiter is not supported</li>
778  * </ul>
779  */
780 @Override
replaceTextRange(int start, int replaceLength, String newText)781 public void replaceTextRange(int start, int replaceLength, String newText){
782 	// check for invalid replace operations
783 	if (!isValidReplace(start, replaceLength, newText)) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
784 
785 	// inform listeners
786 	StyledTextEvent event = new StyledTextEvent(this);
787 	event.type = ST.TextChanging;
788 	event.start = start;
789 	event.replaceLineCount = lineCount(start, replaceLength);
790 	event.text = newText;
791 	event.newLineCount = lineCount(newText);
792 	event.replaceCharCount = replaceLength;
793 	event.newCharCount = newText.length();
794 	sendTextEvent(event);
795 
796 	// first delete the text to be replaced
797 	delete(start, replaceLength, event.replaceLineCount + 1);
798 	// then insert the new text
799 	insert(start, newText);
800 	// inform listeners
801 	event = new StyledTextEvent(this);
802 	event.type = ST.TextChanged;
803 	sendTextEvent(event);
804 }
805 /**
806  * Sends the text listeners the TextChanged event.
807  */
sendTextEvent(StyledTextEvent event)808 void sendTextEvent(StyledTextEvent event) {
809 	for (StyledTextListener textListener : textListeners) {
810 		textListener.handleEvent(event);
811 	}
812 }
813 /**
814  * Sets the content to text and removes the gap since there are no sensible predictions
815  * about where the next change will occur.
816  * <p>
817  *
818  * @param text the text
819  */
820 @Override
setText(String text)821 public void setText (String text){
822 	textStore = text.toCharArray();
823 	gapStart = -1;
824 	gapEnd = -1;
825 	expandExp = 1;
826 	indexLines();
827 	StyledTextEvent event = new StyledTextEvent(this);
828 	event.type = ST.TextSet;
829 	event.text = "";
830 	sendTextEvent(event);
831 }
832 /**
833  * Deletes text.
834  * <p>
835  * @param position the position at which the text to delete starts
836  * @param length the length of the text to delete
837  * @param numLines the number of lines that are being deleted
838  */
delete(int position, int length, int numLines)839 void delete(int position, int length, int numLines) {
840 	if (length == 0) return;
841 
842 	int startLine = getLineAtOffset(position);
843 	int startLineOffset = getOffsetAtLine(startLine);
844 	int endLine = getLineAtOffset(position + length);
845 
846 	String endText = "";
847 	boolean splittingDelimiter = false;
848 	if (position + length < getCharCount()) {
849 		endText = getTextRange(position + length - 1, 2);
850 		if ((endText.charAt(0) == SWT.CR) && (endText.charAt(1) == SWT.LF)) {
851 			splittingDelimiter = true;
852 		}
853 	}
854 
855 	adjustGap(position + length, -length, startLine);
856 	int [][] oldLines = indexLines(position, length + (gapEnd - gapStart), numLines);
857 
858 	// enlarge the gap - the gap can be enlarged either to the
859 	// right or left
860 	if (position + length == gapStart) {
861 		gapStart -= length;
862 	} else {
863 		gapEnd += length;
864 	}
865 
866 	// figure out the length of the new concatenated line, do so by
867 	// finding the first line delimiter after position
868 	int j = position;
869 	boolean eol = false;
870 	while (j < textStore.length && !eol) {
871 		if (j < gapStart || j >= gapEnd) {
872 			char ch = textStore[j];
873 			if (isDelimiter(ch)) {
874 				if (j + 1 < textStore.length) {
875 					if (ch == SWT.CR && (textStore[j+1] == SWT.LF)) {
876 						j++;
877 					}
878 				}
879 				eol = true;
880 			}
881 		}
882 		j++;
883 	}
884 	// update the line where the deletion started
885 	lines[startLine][1] = (position - startLineOffset) + (j - position);
886 	// figure out the number of lines that have been deleted
887 	int numOldLines = oldLines.length - 1;
888 	if (splittingDelimiter) numOldLines -= 1;
889 	// shift up the lines after the last deleted line, no need to update
890 	// the offset or length of the lines
891 	for (int i = endLine + 1; i < lineCount; i++) {
892 		lines[i - numOldLines] = lines[i];
893 	}
894 	lineCount -= numOldLines;
895 	gapLine = getLineAtPhysicalOffset(gapStart);
896 }
897 }
898